package leetcode.code0982;

public class Solution {
	public int countTriplets(int[] nums) {
		int len = 1 << 16;
		int[] h = new int[len];
		for (int m : nums)
			for (int n : nums)
				h[m & n]++;
		int ans = 0;
		for (int n : nums)
			for (int mask = n ^ 0xffff, sub = mask; sub > 0; sub = (mask & (sub - 1)))
				ans += h[sub];
		return ans + h[0] * nums.length;
	}
}
